package model.commands.controlflow;

import java.util.List;
import model.Environment;
import model.SyntaxErrorException;
import model.commands.Command;


/**
 * Name:
 * IFELSE value
 * [ trueCommand(s) ]
 * [ falseCommand(s) ]
 * 
 * Description:
 * if value is not 0, runs the trueCommands given in the first list, otherwise
 * runs the falseCommands given in the second list
 * returns the value of the final command executed
 * 
 * @author JLongley
 * @author Erick Gonzalez
 */
public class IfElseCommand extends Command {

    @Override
    public int evaluate(Environment e) throws SyntaxErrorException {
        List<Command> myArguments = getArguments();
        boolean ifCondition = myArguments.get(0).evaluate(e) != 0;
        return ifCondition ? myArguments.get(1).evaluate(e) : myArguments.get(2).evaluate(e);
    }
}
